import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  PermissionApi,
  PermissionModel,
  PermissionKey,
  PermissionPage,
  PermissionReq,
  PermissionCreateReq,
  PermissionUpdateReq,
  PermissionKeyList,
  ResourcePermList,
} from "./types";

class PermissionApiImpl implements PermissionApi {
  getKey(model: PermissionModel): PermissionKey {
    return {
      permId: model.permId,
    } as PermissionKey;
  }

  /**
   * 获取权限列表分页数据
   *
   * @param query 查询参数，可选
   * @returns 返回请求结果
   */
  findPage(query: PermissionReq): AxiosPromise<PermissionPage> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/perm/page",
      method: "get",
      params: query,
    });
  }

  // 查询权限表
  findById(query: PermissionKey): AxiosPromise<PermissionModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/perm/id",
      method: "get",
      params: query,
    });
  }

  /**
   * 删除权限
   *
   * @param permId 权限ID
   * @returns 返回请求结果
   */
  delete(query: PermissionKey): AxiosPromise<PermissionModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/perm/id",
      method: "delete",
      params: query,
    });
  }

  /**
   * 添加权限
   *
   * @param data 权限数据
   * @returns 返回请求结果
   */
  create(data: PermissionCreateReq): AxiosPromise<PermissionModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/perm",
      method: "post",
      data: data,
    });
  }

  /**
   * 编辑权限
   *
   * @param data 编辑权限所需数据
   * @returns 返回请求结果
   */
  update(data: PermissionUpdateReq): AxiosPromise<PermissionModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/perm",
      method: "patch",
      data: data,
    });
  }
  /**
   * 批量删除权限
   *
   * @param ids 权限ID数组
   * @returns 返回请求结果
   */
  batchDelete(keys: PermissionKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.permId);
    const data: PermissionKeyList = {
      tenant: useUserStore().user.tenant,
      permId: ids,
    } as PermissionKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/perm/batch",
      method: "put",
      data: data,
    });
  }

  /**
   * 权限校验 API
   *
   * @param permCode 权限编码
   * @returns 返回请求结果
   */
  checkPermApi(permCode: string): AxiosPromise {
    return request({
      url: "/v1/rbac/perm/check",
      method: "get",
      params: {
        tenant: useUserStore().user.tenant,
        permCode: permCode,
      },
    });
  }

  /**
   * 获取权限列表
   *
   * @param moduleId 模块ID
   * @returns 返回一个Promise，resolve为权限列表
   */
  permlist(moduleId?: string): AxiosPromise<ResourcePermList> {
    const query = {
      tenant: useUserStore().user.tenant,
      moduleId: moduleId,
    };
    return request({
      url: "/v1/rbac/perm/permlist",
      method: "get",
      params: query,
    });
  }
}
const permissionApi = new PermissionApiImpl();
export { permissionApi };
